{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "@courses_bp.route(\"/delete/model\", methods=[\"post\"])\n",
    "def delete_model():\n",
    "    \"\"\"\n",
    "    删除模型,可删除单个模型(code = 0)、某方向模型(code = 1)、以及不可用模型(code = 2)\n",
    "    -- notice: 暂时未实现在删除模型时把其对应的调用记录一并删除 --\n",
    "    :return:\n",
    "    \"\"\"\n",
    "\n",
    "    # eg: {\"code\": \"0\", \"info\":{\"modelname\": \"user2\", \"modelid\": \"4\", \"category\":\"SA\", \"status\": \"0\"}}\n",
    "    code = request.json.get(\"code\").strip()\n",
    "\n",
    "    try:\n",
    "        if code == \"0\":\n",
    "            modelname = request.json.get(\"info\").get(\"modelname\")\n",
    "            modelid = request.json.get(\"info\").get(\"modelid\")\n",
    "            if modelname:\n",
    "                sql = \"delete from models where Modelname = '%s'\" % modelname\n",
    "            else:\n",
    "                sql = \"delete from models where ModelId = %s\" % modelid\n",
    "        elif code == \"1\":\n",
    "            info = request.json.get(\"info\")\n",
    "            category = info.get(\"category\")\n",
    "            if category:\n",
    "                sql = \"delete from models where ModelCate = '%s'\" % category\n",
    "            else:\n",
    "                return jsonify({\"code\": 501, \"msg\": \"删除方向未填写!\"})\n",
    "        elif code == \"2\":\n",
    "            isavailable = request.json.get(\"info\").get(\"status\")\n",
    "            if isavailable == \"\":\n",
    "                return jsonify({\"code\": 501, \"msg\": \"删除状态未填写!\"})\n",
    "            elif isavailable == \"0\":\n",
    "                sql = \"delete from models where IsAvailable = 0\"\n",
    "            else:\n",
    "                category = request.json.get(\"info\").get(\"category\")\n",
    "                if category != \"\":\n",
    "                    sql = (\n",
    "                        \"delete from models where IsAvailable = 0 and ModelCate = '%s'\"\n",
    "                        % category\n",
    "                    )\n",
    "                else:\n",
    "                    return jsonify({\"code\": 501, \"msg\": \"删除方向未填写!\"})\n",
    "\n",
    "        cursor.execute(\"set foreign_key_checks = 0\")\n",
    "        res = cursor.execute(sql)\n",
    "        cursor.execute(\"set foreign_key_checks = 1\")\n",
    "        db.commit()\n",
    "    except:\n",
    "        import traceback\n",
    "\n",
    "        traceback.print_exc()\n",
    "\n",
    "    if res:\n",
    "        return jsonify({\"code\": 200, \"msg\": \"删除成功!\"})\n",
    "    return jsonify({\"code\": 500, \"msg\": \"删除失败!\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "@courses_bp.route(\"/delete/invoking\", methods=[\"Post\"])\n",
    "def delete_invoking_single():\n",
    "    \"\"\"\n",
    "    删除单条调用记录，可以根据参数删除invokings或各方向调用表的任意一条记录\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    \"\"\"\n",
    "    eg:\n",
    "    {\"table\": \"invokings\", \"invokeid\": \"1\"}\n",
    "    或\n",
    "    {\"table\": \"stinvokings\", \"invokeid\": \"3\"}\n",
    "    \"\"\"\n",
    "\n",
    "    table = request.json.get(\"table\")\n",
    "    invokeid = request.json.get(\"invokeid\")\n",
    "    orientation = (\"stinvokings\", \"sainvokings\", \"mctinvokings\", \"hrinvokings\")\n",
    "    res = 0\n",
    "    try:\n",
    "        tag = 0\n",
    "        if table == \"invokings\":\n",
    "            sql = \"delete from invokings where InvokeId = '%s'\" % invokeid\n",
    "            tag = 1\n",
    "        for i in range(4):\n",
    "            if table == orientation[i]:\n",
    "                sql0 = \"select InvokeId from %s where %sId = %s\" % (\n",
    "                    orientation[i],\n",
    "                    orientation[i][0:2],\n",
    "                    invokeid,\n",
    "                )\n",
    "                cursor.execute(sql0)\n",
    "                data = cursor.fetchone()\n",
    "                if data != None:\n",
    "                    sql1 = \"delete from %s where %sId = %s\" % (\n",
    "                        orientation[i],\n",
    "                        orientation[i][0:2],\n",
    "                        invokeid,\n",
    "                    )\n",
    "                    sql = \"delete from invokings where InvokeId = %s\" % data\n",
    "                    res += cursor.execute(sql1)\n",
    "                tag = 2\n",
    "        if not tag:\n",
    "            return jsonify({\"code\": 501, \"msg\": \"表名错误!\"})\n",
    "        cursor.execute(\"set foreign_key_checks = 0\")\n",
    "        res += cursor.execute(sql)\n",
    "        cursor.execute(\"set foreign_key_checks = 1\")\n",
    "        db.commit()\n",
    "    except:\n",
    "        import traceback\n",
    "\n",
    "        traceback.print_exc()\n",
    "\n",
    "    if res == tag:\n",
    "        return jsonify({\"code\": 200, \"msg\": \"删除成功!\"})\n",
    "    elif res and res < tag:\n",
    "        return jsonify({\"code\": 501, \"msg\": \"部分删除成功!\"})\n",
    "    return jsonify({\"code\": 500, \"msg\": \"删除失败!\"})\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
